package com.controller.sys;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import net.sf.json.JSONArray;
//
//import org.apache.commons.dbutils.DbUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.common.DateUtils;
import com.common.DesUtils;
import com.common.wx.WXUtil;
import com.dao.sys.SysManageMapper;
import com.dao.sys.SysUserMapper;
import com.dao.task.TaskMapper;
import com.model.sys.SysUser;

@Controller
public class LoginController {
	static final Logger logger = LoggerFactory.getLogger(LoginController.class);

	@Autowired
	private SysUserMapper sysUserMapper;
	@Autowired
	private SysManageMapper sysManageMapper;

	@Autowired

	@RequestMapping("/sys/login")
	public String login(HttpServletRequest request) {
		String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1a8f3deaac701eda&redirect_uri=http://localhost:8060/Baseframe&response_type=code&scope=snsapi_userinfo&state=111#wechat_redirect";
//		String code=HttpClientUtil.sendGet(url, "");
//		System.out.println(code);
		return "sys/loginAMZ";
	}

	@RequestMapping("/sys/logout")
	public String logout(HttpServletRequest request) {
		request.getSession().invalidate();
		return "sys/logout";
	}

	@RequestMapping("/sys/ChangePwd")
	public String ChangePwd(HttpServletRequest request) {
		return "sys/ChangePwd";
	}

	@RequestMapping("/sys/default")
	public String sysdefault(HttpServletRequest request) {
		return "sys/default";
	}

	@RequestMapping(value = "/sys/loginin", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String sysloginin(HttpServletRequest request) {
		try {
			String loginnameString = request.getParameter("loginname");
			loginnameString = loginnameString == null ? "" : loginnameString;
			String pwdString = request.getParameter("pwd");
			pwdString = pwdString == null ? "" : pwdString;
			SysUser sysUser = sysUserMapper.selectByNamePwd(loginnameString, DesUtils.encrypt(pwdString));
			if (sysUser == null) {
				return "0";
			} else {
				// JSONArray jsonarray = JSONArray.fromObject(sysUser);
				String puserString = JSON.toJSONString(sysUser);
				request.getSession().setAttribute("display_user", puserString);
				request.getSession().setAttribute("sysuser", sysUser);
				request.getSession().setAttribute("display_userid", sysUser.getUserid());
				return "1";
			}
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return e.getMessage();

		}
	}

	@RequestMapping(value = "/loginMobile")
	public String loginMobile(HttpServletRequest request) {
		try {
			// 通过微信code获取登录帐号
			String code = request.getParameter("code");
			code = code == null ? "" : code;
			String pwdString = request.getParameter("pwd");
			pwdString = pwdString == null ? "" : pwdString;
			WXUtil wxUtil = new WXUtil();
			String loginnameString = wxUtil.getUserId(code);
			// 通过hi获取用户信息
//			loginnameString = "chenjun";
			HashMap<String, Object> hashMap = new HashMap<>();
			hashMap.put("casid", loginnameString);
			SysUser sysUser = sysUserMapper.selectByCasName(hashMap);
			if (sysUser == null) {
				return "sys/loginAMZ";
			} else {
				String puserString = JSON.toJSONString(sysUser);
				request.getSession().setAttribute("display_user", puserString);
				request.getSession().setAttribute("sysuser", sysUser);
				request.getSession().setAttribute("display_userid", sysUser.getUserid());
				request.setAttribute("currentyear", DateUtils.getCurrentYear());
				request.setAttribute("currentweek", DateUtils.getWeek(new Date()));
				request.setAttribute("currentdate", DateUtils.getCurrentDate());
				HashMap<String, Object> hashtable = new HashMap<>();
				int week = DateUtils.getWeek(new Date());
				String year = DateUtils.getCurrentYear();
				hashtable.put("week", year + week);
				hashtable.put("userid", sysUser.getUserid());
				hashtable.put("username", sysUser.getUsername());
				List<HashMap<String, Object>> list = taskMapper.selectMytask(hashtable);
				request.setAttribute("taskList", list);
				return "wx/taskList";
			}
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return e.getMessage();

		}
	}

	@Autowired
	private TaskMapper taskMapper;

	@RequestMapping(value = "/logincas")
	public String logincas(HttpServletRequest request) {
		try {
			String loginnameString = request.getParameter("username");
			loginnameString = loginnameString == null ? "" : loginnameString;
			String pwdString = request.getParameter("pwd");
			pwdString = pwdString == null ? "" : pwdString;
			HashMap<String, Object> hashMap = new HashMap<>();
			hashMap.put("casid", loginnameString);
			SysUser sysUser = sysUserMapper.selectByCasName(hashMap);
			if (sysUser == null) {
				return "sys/loginAMZ";
			} else {
				String puserString = JSON.toJSONString(sysUser);
				request.getSession().setAttribute("display_user", puserString);
				request.getSession().setAttribute("sysuser", sysUser);
				request.getSession().setAttribute("display_userid", sysUser.getUserid());
				request.setAttribute("currentyear", DateUtils.getCurrentYear());
				request.setAttribute("currentweek", DateUtils.getWeek(new Date()));
				request.setAttribute("currentdate", DateUtils.getCurrentDate());
				return "task/TaskStatis";
			}
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return e.getMessage();

		}
	}

	@RequestMapping("/common/error")
	public String commonerror(HttpServletRequest request) {
		try {
			// System.out.println(1/0);
			return "common/error";
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("errorMessage:{}", e.getMessage());
			request.setAttribute("errorMessage", "" + e.getMessage());
			return "common/error";

		}
	}
}
